package com.example.vue.sys.service.impl;

import com.example.vue.comm.domain.PageBean;
import com.example.vue.comm.domain.ResultData;
import com.example.vue.comm.util.IdUtil;
import com.example.vue.sys.domain.SysRole;
import com.example.vue.sys.mapper.SysRoleMapper;
import com.example.vue.sys.service.SysRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 角色管理 service层
 *
 * @author zhouxl
 * @DATE 2020/12/9
 * @descriptoin
 */
@Component
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Object addRole(Map<String, Object> params) {
        ResultData success = ResultData.success(null);
        String roleName = (String) params.get("roleName");
        String roleCode = (String) params.get("roleCode");
        SysRole role = new SysRole();
        role.setRoleName(roleName);
        role.setRoleCode(roleCode);
        role.setRoleNo(IdUtil.idByDate(new Date(), "yyyyMMddhhmmss", null));
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        int row = sysRoleMapper.addRole(role);
        success.setMessage("角色添加成功");
        return success;
    }

    @Override
    public Object queryRoleList(Map<String, Object> params) {
        ResultData data = ResultData.success(null);
        int pageNum = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));
        PageHelper.startPage(pageNum, limit);
        Page<Map<String, Object>> rolePage = sysRoleMapper.queryRoleList(params);
        PageBean pageBean = new PageBean(rolePage);
        data.setData(pageBean);
        return data;
    }

    @Override
    public Object editRole(Map<String, Object> params) {
        ResultData success = ResultData.success(null);
        SysRole role = sysRoleMapper.queryRole(params);
        if (Objects.isNull(role)) {
            success.setCode(400);
            success.setMessage("角色编辑失败,编辑的角色信息不存在");
            return success;
        }
        role.setRoleName((String) params.get("roleName"));
        role.setRoleCode((String) params.get("roleCode"));
        role.setUpdateTime(new Date());
        int row = sysRoleMapper.editRole(role);
        success.setData(null);
        success.setMessage("编辑成功");
        return success;
    }

    @Override
    public Object deleteRole(Map<String, Object> params) {
        ResultData success = ResultData.success(null);
        // 删除角色信息
        int row = sysRoleMapper.deleteRole(params);
        // 删除权限角色信息
        row += sysRoleMapper.deleteRoleMenu(params);
        success.setCode(row == 0 ? 400 : 200);
        success.setMessage(row == 0 ? "角色删除失败" : "角色删除成功");
        return success;
    }

    @Override
    public Object queryRoleMenuTree(Map<String, Object> params) {
        ResultData success = ResultData.success(null);
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> roleMenuList = sysRoleMapper.queryRoleMenuTree(params);
        // 获取选中的菜单信息
        List<Object> selectMenuNo = roleMenuList.stream()
                .filter(x -> (long) x.get("flag") == 1)
                .map(x -> x.get("menuNo"))
                .collect(Collectors.toList());
        result.put("selectMenu", selectMenuNo);
        result.put("expandMenu", selectMenuNo);
        List<Map<String, Object>> subMenuTree = new ArrayList<>();
        subRoleMenuTree(roleMenuList, subMenuTree, 1);
        result.put("menuTree", subMenuTree);
        success.setData(result);
        return success;
    }

    @Override
    public Object saveRoleMenu(Map<String, Object> params) {
        /*
        先删除该角色所有的菜单权限
        添加该角色的菜单权限
         */
        ResultData success = ResultData.success(null);
        String menuNos = (String) params.get("menuNos");
        String[] split = menuNos.split(",");
        // 删除角色信息
        sysRoleMapper.deleteRoleMenu(params);
        if (split.length == 1 && "".equals(split[0])) {
            success.setCode(300);
            success.setMessage("当前角色的所有权限信息置空");
            return success;
        } else {
            // 添加角色信息
            for (int i = 0; i < split.length; i++) {
                params.put("menuNo", split[i]);
                Map<String, Object> roleMenu = sysRoleMapper.queryRoleMenu(params);
                if (Objects.isNull(roleMenu)) {
                    params.put("id", IdUtil.uuid());
                    int row = sysRoleMapper.saveRoleMenu(params);
                }
            }
        }
        success.setMessage("权限信息保存成功");
        return success;
    }

    @Override
    public Object querySelectRoleList(Map<String, Object> params) {
        ResultData data = ResultData.success(null);
        List<Map<String, Object>> roleList = sysRoleMapper.querySelectRoleList(params);
        data.setData(roleList);
        return data;
    }

    /**
     * 通过查询的菜单信息，构建菜单树结构信息
     *
     * @param roleMenuList
     * @param subRoleMenuTree
     * @param grade
     */
    private void subRoleMenuTree(List<Map<String, Object>> roleMenuList, List<Map<String, Object>> subRoleMenuTree, int grade) {
        if (Objects.isNull(roleMenuList) || roleMenuList.size() == 0) {
            return;
        }
        if (grade == 1) {
            Iterator<Map<String, Object>> it = roleMenuList.iterator();
            while (it.hasNext()) {
                Map<String, Object> next = it.next();
                if ((int) next.get("grade") == grade) {
                    subRoleMenuTree.add(next);
                    it.remove();
                }
            }
            subRoleMenuTree(roleMenuList, subRoleMenuTree, grade + 1);
        } else {
            for (Map<String, Object> map : subRoleMenuTree) {
                String menuNo = (String) map.get("menuNo");
                Iterator<Map<String, Object>> it = roleMenuList.iterator();
                List<Map<String, Object>> subMenuList = new ArrayList<>();
                while (it.hasNext()) {
                    Map<String, Object> next = it.next();
                    String nextPmenuNo = (String) next.get("pMenuNo");
                    if (menuNo.equals(nextPmenuNo)) {
                        subMenuList.add(next);
                        it.remove();
                    }
                }
                map.put("treeList", subMenuList);
                subRoleMenuTree(roleMenuList, subMenuList, grade + 1);
            }
        }

    }


}
